﻿Utilities = {	 
};

Utilities.checkFileAPIs = function()
{
	if (window.File && window.FileReader && window.FileList && window.Blob) 
	{
		return true;
	} else 
	{
		return false;
	}
};

Utilities.sleep = function(ms)
{
	var dt = new Date();
	dt.setTime(dt.getTime() + ms);
	while (new Date().getTime() < dt.getTime());
};

Utilities.get2DContext = function(canvasName)
{
	var ctx = document.getElementById(canvasName).getContext('2d');
	return ctx;	
};

Utilities.setImageToCanvas = function(canvasName, image, isScale)
{
	$("#" + canvasName).show();
	var ctx = document.getElementById(canvasName).getContext('2d');
	
	if (isScale)
	{
		$("#" + canvasName).attr('width', image.width);
		$("#" + canvasName).attr('height', image.height);
	}
	ctx.putImageData(image, 0, 0);
};

Utilities.getGeoLocation = function()
{
	if (navigator.geolocation) 
	{
		navigator.geolocation.getCurrentPosition
		(
				function(position) 
				{
					startPos = position;
					alert('Location: (' + startPos.coords.latitude.toString() + ',' + startPos.coords.longitude.toString() + ')');
					
					//document.getElementById("startLat").innerHTML = startPos.coords.latitude;
					//document.getElementById("startLon").innerHTML = startPos.coords.longitude;
				}, 
				function(error) 
				{
					alert("Error occurred. Error code: " + error.code);
					// error.code can be:
					//   0: unknown error
					//   1: permission denied
					//   2: position unavailable (error response from location provider)
					//   3: timed out
				}
		);
	}
};


Utilities.parseParams = function(str)
{
	// params = [param1,param2,...]
	str = str.replace(" ", "");
	var params = str.split(",");
	
	return params;
};

Utilities.capitaliseFirstLetter = function(string)
{
    return string[0].toUpperCase() + string.slice(1);
}


Utilities.checkLength = function (string, min, max)
{
	if (typeof string != 'string')
		return false;
	
	if (string.length < min)
		return false;
	
	if (max > 0 && string.length > max)
		return false;
	
	return true;
}